package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.spi.ResolveResult;
import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search")
@SuppressWarnings({"all"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;


    /**
     * 仅用于测试-将指定已存在专辑上架保存到索引库
     *
     * @param albumId
     * @return
     */
    @Operation(summary = "仅用于测试-将指定已存在专辑上架保存到索引库")
    @GetMapping("/albumInfo/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable Long albumId) {
        searchService.upperAlbum(albumId);
        return Result.ok();
    }


    /**
     * 仅用于测试-将指定专辑从索引库中删除
     *
     * @param albumId
     * @return
     */
    @Operation(summary = "仅用于测试-将指定专辑从索引库中删除")
    @GetMapping("/albumInfo/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable Long albumId) {
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }


    /**
     * 专辑站内检索：支持关键字、分类及标签过滤，分页，高亮
     *
     * @param queryVo
     * @return
     */
    @Operation(summary = "专辑站内检索：支持关键字、分类及标签过滤，分页，高亮")
    @PostMapping("/albumInfo")
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery queryVo) {
        AlbumSearchResponseVo responseVo = searchService.search(queryVo);
        return Result.ok(responseVo);
    }


    /**
     * 查询指定一级分类下前7个三级分类，得到每个三级分类下热度前6的热门专辑
     *
     * @param category1Id 1级分类ID
     * @return
     */
    @Operation(summary = "查询指定一级分类下前7个三级分类，得到每个三级分类下热度前6的热门专辑")
    @GetMapping("/albumInfo/channel/{category1Id}")
    public Result<List<Map<String, Object>>> searchCategory3Top6(@PathVariable Long category1Id) {
        List<Map<String, Object>> list = searchService.searchCategory3Top6(category1Id);
        return Result.ok(list);
    }


    /**
     * 根据已填写关键字返回自动补全内容
     * @param keyword
     * @return
     */
    @Operation(summary = "根据已填写关键字返回自动补全内容")
    @GetMapping("/albumInfo/completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable String keyword){
        List<String> list = searchService.completeSuggest(keyword);
        return Result.ok(list);
    }


    /**
     * 仅用于测试-手动将不同分类下ES热门专辑存入Redis中
     * @return
     */
    @Operation(summary = "仅用于测试-手动将不同分类下ES热门专辑存入Redis中")
    @GetMapping("/albumInfo/updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking(){
        searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }

    /**
     * 根据分类ID及热度方式查询专辑列表
     * @param category1Id
     * @param dimension
     * @return
     */
    @Operation(summary = "根据分类ID及热度方式查询专辑列表")
    @GetMapping("/albumInfo/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndex>> getRankingList(@PathVariable Long category1Id, @PathVariable String dimension){
        List<AlbumInfoIndex> list = searchService.getRankingList(category1Id, dimension);
        return Result.ok(list);
    }

}

